<?php

if(!$this->CheckPermission($this->GetName().' Users')){
    Antz::addError($this->Lang('insufficient_permissions'));
    Antz::redir($actionUrls['edit']);
}


if(Antz_IntelliForm::submitted('ISubscribe_AddUser')){

    if(strlen(post('addBtn'))>0){
        // trying to add a user
        $error = false;
        $userid = post('add_userid');
        $subscriptionid = (int) post('subscriptionid');

        if($userid === 'none'){
            $error = true;
            Antz::addError($this->Lang('invalid_user'));
        }

        $userid = (int) $userid;


        if($DB->countRows($this->config['db_prefix'].'iusers', "id = '{$userid}'")==0){
            $error = true;
            Antz::addError($this->Lang('invalid_user'));
        }
        
        if($DB->countRows($this->config['db_prefix'].'isubscribe_subscriptions', "id = '{$subscriptionid}'")==0){
            $error = true;
            Antz::addError($this->Lang('invalid_subscription'));
        }

        if($DB->countRows($this->config['db_prefix'].'isubscribe_users_to_subscriptions', "user_id = '{$userid}' AND subscription_id = '{$subscriptionid}'")>0){
            $error = true;
            Antz::addError($this->Lang('error_already_subscribed'));
        }

        if(!$error){
            // its ok to add
            $gwObj = new stdClass();
            $priceRow = $DB->fetchRow("SELECT * FROM {$this->config['db_prefix']}isubscribe_subscription_prices WHERE subscription_id = '{$subscriptionid}' LIMIT 1");
            $subRow = $DB->fetchRow("SELECT * FROM {$this->config['db_prefix']}isubscribe_subscriptions WHERE id = '{$subscriptionid}'");
            $expireTstamp = $this->AddSubscription($priceRow, $subRow, $gwObj, $userid);
            Antz::addSuccess($this->lang('user_added_success'));
        }
    }


    if(strlen(post('delBtn'))>0){
        // able to delete a user
        $userid = post('del_userid');
        $subscriptionid = (int) post('subscriptionid');

        if($userid === 'none'){
            $error = true;
            Antz::addError($this->Lang('invalid_user'));
        }

        $userid = (int) $userid;

        if($DB->countRows($this->config['db_prefix'].'iusers', "id = '{$userid}'")==0){
            $error = true;
            Antz::addError($this->Lang('invalid_user'));
        }
        
        if($DB->countRows($this->config['db_prefix'].'isubscribe_subscriptions', "id = '{$subscriptionid}'")==0){
            $error = true;
            Antz::addError($this->Lang('invalid_subscription'));
        }

        if($DB->countRows($this->config['db_prefix'].'isubscribe_users_to_subscriptions', "user_id = '{$userid}' AND subscription_id = '{$subscriptionid}'")==0){
            $error = true;
            Antz::addError($this->Lang('error_not_subscribed'));
        }


        
        if(!$error){
            // its ok to add
            $expireTstamp = time()-1;
            $DB->delete($this->config['db_prefix'].'isubscribe_users_to_subscriptions', "user_id = '{$userid}' AND subscription_id = '{$subscriptionid}'");
            Antz::addSuccess($this->lang('user_deleted_success'));
        }

    }


}




Antz::redir($actionUrls['edit']);












